import re
import requests
import sys

# 定义请求头
HEADERS = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
    "Accept-Encoding": "gzip, deflate, br",
    "Upgrade-Insecure-Requests": "1",
    "Sec-Fetch-Dest": "document",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-Site": "none",
    "Sec-Fetch-User": "?1",
    "Priority": "u=0, i",
    "Te": "trailers",
    "Connection": "keep-alive",
}

def extract_domains_and_ips(response_content):
    # 正则表达式匹配域名和IP地址
    domain_pattern = r'https?://([a-zA-Z0-9.-]+)'
    ip_pattern = r'\b(?:\d{1,3}\.){3}\d{1,3}\b'

    # 查找所有匹配的域名和IP地址
    domains = re.findall(domain_pattern, response_content)
    ips = re.findall(ip_pattern, response_content)

    # 去重
    unique_domains = list(set(domains))
    unique_ips = list(set(ips))

    return unique_domains, unique_ips

def main(url):
    try:
        # 发送HTTP GET请求，添加请求头
        response = requests.get(url, timeout=10,headers=HEADERS)
        response.raise_for_status()  # 检查请求是否成功

        # 获取响应内容
        response_content = response.text

        # 提取域名和IP地址
        domains, ips = extract_domains_and_ips(response_content)

        # 打印结果
        print("Unique Domains:")
        for domain in domains:
            print(domain)

        print("\nUnique IPs:")
        for ip in ips:
            print(ip)

    except requests.exceptions.RequestException as e:
        print(f"Error fetching URL: {e}")

if __name__ == "__main__":
    # 检查是否提供了命令行参数
    if len(sys.argv) != 2:
        print("Usage: python test.py <URL>")
        sys.exit(1)

    # 获取命令行参数中的URL
    target_url = sys.argv[1]

    # 运行主函数
    main(target_url)